<?php

// +----------------------------------------------------------------------
// | 合同模板服务层
// +----------------------------------------------------------------------
// | 主要服务于合同模板配置
// +----------------------------------------------------------------------
// | 错误编码头 236xxx
// +----------------------------------------------------------------------
namespace common\service;


use common\exception\LogicException;
use support\ErrorCode;
use support\SingletonTrait;
use think\Exception;
use common\model\ContractTemplateModel;

class ContractTemplateService
{
    // 单例工具
    use SingletonTrait;

    /**
     * 创建模板
     * @param $data
     * @param \think\File $tplFile
     * @return mixed
     * @throws \Throwable
     */
    public function create($data, \think\File $tplFile)
    {
        $contractTemplateModel = model(ContractTemplateModel::class);

        $data['template_id'] = md5(create_uuid());
        $contractTemplateModel->startTrans();
        try {
            $res = $contractTemplateModel->addItem($data);
            if (!$res) {
                throw_strack_exception($contractTemplateModel->getError(), $contractTemplateModel->getErrorCode());
            }
            $fddService = FDDService::getInstance();
            $fddService->uploadContractTemplate(['template_id' => $data['template_id']], $tplFile->getPathname());
            $contractTemplateModel->commit();
            return $res;
        } catch (\Throwable $e) {
            $contractTemplateModel->rollback();
            throw $e;
        }
    }

    /**
     * 删除模板
     * @param $param
     * @return mixed
     * @throws \Throwable
     */
    public function delete($param)
    {
        $contractTemplateModel = model(ContractTemplateModel::class);
        $fddService = FDDService::getInstance();
        $res = [];
        $contractTemplateModel->startTrans();
        try {
            $param['fields'] = "id,template_id";
            $templates = $contractTemplateModel->selectData($param);

            foreach ($templates['rows'] as $row) {
                $flag = $contractTemplateModel->deleteItem(['id' => $row['id']]);
                if (!$flag) {
                    throw_strack_exception($contractTemplateModel->getError(), $contractTemplateModel->getErrorCode());
                }
                $res[] = $row['id'];
                $fddService->deleteTemplate(['template_id' => $row['template_id']]);
            }
            $contractTemplateModel->commit();
            return $res;
        } catch (\Throwable $e) {
            $contractTemplateModel->rollback();
            throw $e;
        }
    }

    /**
     * 查看模板 返回一个模板预览链接
     * @param $param
     * @return string
     */
    public function view($param)
    {
        $contractTemplateModel = model(ContractTemplateModel::class);
        $contractTemplate = $contractTemplateModel->findData($param);
        if (empty($contractTemplate)) {
            throw_strack_exception("can`t found match template", ErrorCode::NO_MATCHING_TEMPLATE);
        }
        $templateId = $contractTemplate['template_id'];
        $fddService = FDDService::getInstance();
        return $fddService->viewTemplate(['template_id' => $templateId]);
    }

    /**
     * 查看合同模板可用keys
     * @param $param
     * @return mixed
     * @throws Exception
     */
    public function getTemplateKeys($param)
    {
        $contract = model(ContractTemplateModel::class)->findData($param);

        if (empty($contract)) {
            throw new LogicException("can`t found match record", ErrorCode::NO_MATCH_RECORD);
        }
        $fddService = FDDService::getInstance();
        $contract['template_available_keys'] = $fddService->getContractTemplateKeys($contract['template_id']);
        return $contract;
    }
}